package com.wqiuqiu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.wqiuqiu.common.PageUtils;
import com.wqiuqiu.common.R;
import com.wqiuqiu.gulimall.product.entity.SpuInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wqiuqiu.gulimall.product.entity.SkuInfoEntity;
import com.wqiuqiu.gulimall.product.service.SkuInfoService;



/**
 * sku信息
 *
 * @author wqiuqiu
 * @email 18773489795@163.com
 * @date 2023-12-11 12:32:41
 */
@RestController
@RequestMapping("product/skuinfo")
public class SkuInfoController {
    @Autowired
    private SkuInfoService skuInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = skuInfoService.queryPage(params);

        return R.ok().put("page", page);
    }

    public R skuInfoBlockHandler(Long skuId, BlockException e){
        System.out.println("发生限流。。。");
        return null;
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{skuId}")
    @SentinelResource(value = "skuIdInfo",blockHandler = "skuInfoBlockHandler")
    public R info(@PathVariable("skuId") Long skuId){
		SkuInfoEntity skuInfo = skuInfoService.getById(skuId);

        return R.ok().setData(skuInfo);
    }

    @PostMapping("/infos")
    public R batchInfobyIds(@RequestBody List<Long> skuIds){
        List<SkuInfoEntity> skuInfos = skuInfoService.batchInfobyIds(skuIds);

        return R.ok().setData(skuInfos);
    }



    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SkuInfoEntity skuInfo){
		skuInfoService.save(skuInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SkuInfoEntity skuInfo){
		skuInfoService.updateById(skuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] skuIds){
		skuInfoService.removeByIds(Arrays.asList(skuIds));

        return R.ok();
    }

}
